import util  from './utils/util.js'
import api from './utils/api.js'
import user from './utils/user.js'

App({
  onLaunch() {
    //获取用户的登录信息
    user.checkLogin().then(res => {
      this.globalData.userInfo = wx.getStorageSync('userInfo')
      this.globalData.uid = wx.getStorageSync('uid')
    }).catch(() => {

    })
    this.goLogin()
  },
  goLogin() {
    let that = this
    user.loginByWeixin().then(res => {
      that.globalData.userInfo = res.data.userInfo
      that.globalData.uid = res.data.uid
      wx.setStorageSync('userInfo', res.data.userInfo)
      wx.setStorageSync('uid', res.data.token)
    }).catch((err) => {
      console.log(err)
    })
  },
  globalData: {
    userInfo: {
      nickname: 'Hi,游客',
      username: '点击去登录',
      avatar: 'http://yanxuan.nosdn.127.net/8945ae63d940cc42406c3f67019c5cb6.png'
    },
    uid: '',
  },
  upload_file(url, filePath, name, formData, success, fail) {
    var data = null
    wx.uploadFile({
     url: url,filePath:filePath, name:name,
     header: {
     'content-type':'multipart/form-data'
     }, // 设置请求的 header
     formData: formData, // HTTP 请求中其他额外的 form data
     success: function(res){
      if(res.statusCode ==200 ){
          typeof success == "function" && success(res.data);
      } else{
        typeof fail == "function" && fail(res);
      }
     },
     fail: function(res) {
        typeof fail == "function" && fail(res);
      }
    })
  }
})